package com.xiaotu.easypoi.testimport.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;
import com.xiaotu.easypoi.testexport.model.CourseEntity;
import com.xiaotu.easypoi.testimport.model.ImportUserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.bind.SchemaOutputResolver;
import java.util.List;

/**
 * @author 张晓
 * @create 2021-05-18 21:48
 */
@RestController
@CrossOrigin
public class ImportExcelController {

    @Autowired
    private IExcelVerifyHandler iExcelVerifyHandler;

    @RequestMapping("importExcel")
    public void importExcel(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        //表头行数
        importParams.setHeadRows(1);
        //开启校验空行
        importParams.setNeedVerify(true);
        importParams.setVerifyHandler(iExcelVerifyHandler);
        try {
            ExcelImportResult<ImportUserModel> result = ExcelImportUtil.importExcelMore(file.getInputStream(), ImportUserModel.class, importParams);
            List<ImportUserModel> list = result.getList();
            list.stream().forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("importCourseExcel")
    public void importCourseExcel(MultipartFile file) {
        ImportParams importParams = new ImportParams();
        //表格标题行数
        importParams.setTitleRows(1);
        //表格表头行数
        importParams.setHeadRows(2);
        try {
            List<CourseEntity> list = ExcelImportUtil.importExcel(file.getInputStream(), CourseEntity.class, importParams);
            System.out.println("集合长度为：" + list.size());
            list.stream().forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
